#include "textItemWidget.h"
#include <QApplication>
#include <QScreen>
#include <QWindow>
#include <QMouseEvent>
/**
 * 文本展示组件
 * @brief TextItemWidget::TextItemWidget
 * @param item
 * @param parent
 */

TextItemWidget::TextItemWidget(const ClipboardItem& item, QWidget *parent)
    : QWidget(parent), item(item), textLabel(new QLabel(item.getDisplayText(), this)),
      titleLabel(new QLabel("文本", this)),
      copyButton(new QPushButton("", this)),
      deleteButton(new QPushButton("", this)),
      searchButton(new QPushButton("", this)),
      favoriteButton(new QPushButton("",this)),
      favoriteTextLayout(new QHBoxLayout()),
      mainLayout(new QVBoxLayout(this)),
      buttonLayout(new QHBoxLayout())
{  // 确保按钮布局在初始化列表中
    //设置按钮样式
    copyButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/copy_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/copy_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/copy_button_pressed.png);"
                "}"
                );
    searchButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/search_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
               "image: url(:/icon/icon/search_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/search_button_pressed.png);"
                "}"
                );
    deleteButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/delete_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/delete_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/delete_button_pressed.png);"
                "}"
                );

    // 添加收藏按钮的样式设置
    QString favoriteImageUrl = item.isFavorite() ? 
        ":/icon/icon/favorite_button_pressed.png" : 
        ":/icon/icon/favorite_button.png";
        
    favoriteButton->setStyleSheet(
        "QPushButton { "
        "image: url(" + favoriteImageUrl + ");"
        "width: 15px;"
        "height: 15px;"
        "min-width: 15px;"
        "min-height: 15px;"
        "max-width: 15px;"
        "max-height: 15px;"
        "background-color: transparent;"
        "border: none;"
        "}"
    );

    // 设置布局
    buttonLayout->addWidget(copyButton);
    buttonLayout->addWidget(searchButton);
    buttonLayout->addWidget(deleteButton);

    if (item.getType() == ClipboardType::Text) {
        searchButton->show();  // 确保可见性
        copyButton->show();
    } else {
        searchButton->hide();  // 隐藏非文本项的搜索按钮
        copyButton->hide();    // 隐藏非文本项的复制按钮
    }

    // 设置标题标签的字体和大小
    titleLabel->setStyleSheet("QLabel {font-size: 18px; font-family: 'Dengxian'; font-weight: bold;}"); // 设置标题为粗体
    titleLabel->setContentsMargins(5,5,5,0);



    // 设置文本标签的字体
    textLabel->setStyleSheet("QLabel { font-size: 14px; font-family: 'Dengxian';}");
    textLabel->setContentsMargins(5,0,5,0);
    textLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop); // 设置文本左对齐，顶部对齐

    // 设置文本标签自动换行
    textLabel->setWordWrap(true);

    // 获取主屏幕的分辨率
       QScreen *screen = QGuiApplication::primaryScreen();
       QRect screenGeometry = screen->geometry();
       int screenWidth = screenGeometry.width();
       int screenHeight = screenGeometry.height();
       textLabel->setFixedWidth(screenWidth*0.16);
       textLabel->setMaximumWidth(screenWidth*0.16);
       textLabel->setMaximumHeight(screenHeight*0.10);

       buttonLayout->setContentsMargins(0,0,0,5);

       // 设置标题和收藏按钮的布局
       favoriteTextLayout->addWidget(titleLabel);
       favoriteTextLayout->addStretch(); // 添加弹性空间
       favoriteTextLayout->addWidget(favoriteButton);

       // 修改 mainLayout 中的添加顺序
       mainLayout->addLayout(favoriteTextLayout); // 使用favoriteTextLayout替代直接添加titleLabel
       mainLayout->addWidget(textLabel);
       mainLayout->addStretch();
       mainLayout->addLayout(buttonLayout, 0);

    setLayout(mainLayout);

    // 在 TextItemWidget 本身安装事件过滤器
    this->installEventFilter(this);

    // 连接信号和槽
    connect(copyButton, &QPushButton::clicked, this, &TextItemWidget::onCopyClicked);
    connect(deleteButton, &QPushButton::clicked, this, &TextItemWidget::onDeleteClicked);
    connect(searchButton, &QPushButton::clicked, this, &TextItemWidget::onSearchClicked);
    connect(favoriteButton, &QPushButton::clicked, this, &TextItemWidget::onFavoriteClicked);
}

// 事件过滤器，用于捕获双击事件
bool TextItemWidget::eventFilter(QObject* obj, QEvent* event) {
    if (obj == this && event->type() == QEvent::MouseButtonDblClick) {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        // 获取 mainLayout 的相对位置并转换为全局位置
        QPoint globalPos = this->mapToGlobal(mainLayout->geometry().topLeft());
        QRect mainLayoutRect(globalPos, mainLayout->geometry().size());

        // 检查双击位置是否在 mainLayout 区域内
        if (mainLayoutRect.contains(mouseEvent->globalPos())) {
            onGetDetailDoubleCliked();
            return true;  // 事件已处理
        }
    }
    return QWidget::eventFilter(obj, event);
}

ClipboardItem TextItemWidget::getItem() const {
    return item;  // 返回存储的 ClipboardItem
}

void TextItemWidget::onCopyClicked() {
    emit copyClicked();  // 发射信号
}

void TextItemWidget::onDeleteClicked() {
    emit deleteClicked();  // 发射信号
}

void TextItemWidget::onSearchClicked() {
    emit searchClicked();  // 发射信号
}

void TextItemWidget::onGetDetailDoubleCliked() {
    emit getDetailDoubleCliked();
}

// 添加更新收藏按钮样式的方法
void TextItemWidget::updateFavoriteButtonStyle() {
    QString favoriteImageUrl = item.isFavorite() ? 
        ":/icon/icon/favorite_button_pressed.png" : 
        ":/icon/icon/favorite_button.png";
        
    favoriteButton->setStyleSheet(
        "QPushButton { "
        "image: url(" + favoriteImageUrl + ");"
        "width: 15px;"
        "height: 15px;"
        "min-width: 15px;"
        "min-height: 15px;"
        "max-width: 15px;"
        "max-height: 15px;"
        "background-color: transparent;"
        "border: none;"
        "}"
    );
}

void TextItemWidget::onFavoriteClicked() {
    emit favoriteClicked();
    item.setFavorite(!item.isFavorite());  // 切换收藏状态
    updateFavoriteButtonStyle();  // 立即更新按钮样式
}
